package com.xxz.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginHandlerInterceptor implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //用户登录成功后,应该有自己的session   当有session出现的时候就有用户信息登录了
        Object loginUser = request.getSession().getAttribute("loginUser");
        // 如果  loginUser  为空  就表示没有登录 拦截器就生效
        if (loginUser == null) {
            request.setAttribute("msg", "无登陆权限，请先登录");
            request.getRequestDispatcher("/index.html").forward(request, response);
            return false;
        } else {
            return true;//  拦截器放行
        }
    }
}
